package com.zcf.mappers;

import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class Sort {

	public static void main(String[] args) {
		String filename = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" + "<response>\r\n"
				+ "    <status>1</status>\r\n" + "    <info>OK</info>\r\n" + "    <infocode>10000</infocode>\r\n"
				+ "    <count>1</count>\r\n" + "    <geocodes type=\"list\">\r\n" + "        <geocode>\r\n"
				+ "            <formatted_address>江苏省徐州市铜山区万达</formatted_address>\r\n"
				+ "            <country>中国</country>\r\n" + "            <province>江苏省</province>\r\n"
				+ "            <citycode>0516</citycode>\r\n" + "            <city>徐州市</city>\r\n"
				+ "            <district>铜山区</district>\r\n" + "            <township></township>\r\n"
				+ "            <neighborhood>\r\n" + "                <name></name>\r\n"
				+ "                <type></type>\r\n" + "            </neighborhood>\r\n" + "            <building>\r\n"
				+ "                <name></name>\r\n" + "                <type></type>\r\n"
				+ "            </building>\r\n" + "            <adcode>320312</adcode>\r\n"
				+ "            <street></street>\r\n" + "            <number></number>\r\n"
				+ "            <location>117.188278,34.191573</location>\r\n" + "            <level>兴趣点</level>\r\n"
				+ "        </geocode>\r\n" + "    </geocodes>\r\n" + "</response>";
		String levelCode = filename.substring(filename.indexOf("<location>") + 1).trim();// 逗号后面
		String levelCodes = levelCode.substring(0, levelCode.indexOf("</location>") + 1).trim();// 逗号前面
		String time = "2019-03-08";
		String addtime = time.substring(time.indexOf("-0") + 1).trim();
		String addtimes = addtime.substring(addtime.indexOf("0") + 1).trim();
		System.err.println(addtimes);
		
		String shijian = time.substring(0, time.indexOf("-0") + 1).trim();
		System.err.println(shijian);
		
		String shijian1 = addtimes.substring(addtimes.indexOf("0") + 1).trim();
		System.err.println(shijian1);
		
		String add = addtimes.substring(0, addtimes.indexOf("-0") + 1).trim();
		System.err.println(add);

		shijian=shijian+add+shijian1;
		
		System.err.println(shijian);
//		System.err.println(addtime);
		
//		System.err.println(levelCodes);
//		System.out.println("---------------------java拼接json对象----------------------");
//		Sort ss = new Sort();
//		ss.viewMagazine();
//		System.out.println("---------------------java解析json对象----------------------");
//		strJsonObj();
	}

	/**
	 * 拼json对象
	 */
	public String viewMagazine() {

		Person person = new Person();
		person.setBirth("1989-22-11");
		person.setGrade("07java");
		person.setName("happ");
		person.setSex("boy");

		// 推荐的杂志的结果集
		Person person1 = new Person();
		person1.setBirth("1989-22-11");
		person1.setGrade("07java");
		person1.setName("helloworlda");
		person1.setSex("girl");

		List<Person> list = new ArrayList<Person>();
		list.add(person);
		list.add(person1);

		JSONObject s = new JSONObject();
		JSONArray ss = JSONArray.fromObject(list);// 构建json数组
		// 往json对象中加值
		s.put("person", person);// 添加对象

//		s.put("personlist", list);// 添加数组，list和ss都可以
		s.put("personss", ss);// 添加数组，list和ss都可以
		s.put("comCount", 3);
		System.out.println(s);
		return null;
	}

	/**
	 * java解析json对象，解析出对象和字符串及数组并遍历出相应的值
	 */

	private static void strJsonObj() {
		String json = "{'name': 'helloworlda','array':[{'a':'111','b':'222','c':'333'},{'a':'999'}],'address':'111','people':{'name':'happ','sex':'girl'}}";
		JSONObject jsonobj = JSONObject.fromObject(json);// 将字符串转化成json对象
		String name = jsonobj.getString("name");// 获取字符串。
		JSONArray array = jsonobj.getJSONArray("array");// 获取数组
		JSONObject obj = jsonobj.getJSONObject("people");// 获取对象

		System.out.println("===============strJsonObj==================");
		System.out.println("jsonobj : " + jsonobj);
		System.out.println("array : " + array);
		System.out.println("obj : " + obj.getString("name"));

		// 遍历json对象
		Iterator<?> objkey = obj.keys();
		while (objkey.hasNext()) {// 遍历JSONObject
			String aa2 = (String) objkey.next().toString();
			String bb2 = obj.getString(aa2);
			System.out.println(aa2 + ":" + bb2);
		}
		// 遍历数组
		for (int i = 0; i < array.size(); i++) {
			System.out.println("item " + i + " :" + array.getString(i));
		}
	}

	public class Person {
		private String birth;
		private String grade;
		private String name;
		private String sex;

		public String getBirth() {
			return birth;
		}

		public void setBirth(String birth) {
			this.birth = birth;
		}

		public String getGrade() {
			return grade;
		}

		public void setGrade(String grade) {
			this.grade = grade;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public String getSex() {
			return sex;
		}

		public void setSex(String sex) {
			this.sex = sex;
		}

	}

}
